/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models.dados;

import config.JdbcConnection;
import config.LogErro;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JOptionPane;
import views.utilitario.AtributosDescricao;

/**
 *
 * @author Wenderson
 */
public class JdbcDAO {
    
    private PreparedStatement ps;
    private ResultSet rs;
    
    public int count(String squema, String table) {
        try {
            String sql = "SELECT COUNT(*) FROM " + squema + "." + table;
            
            ps = JdbcConnection.getConnection().prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();
            
            int count = -1;
            
            if (rs.first()) {
                count = rs.getInt(1);
            }
            
            rs.close();
            ps.close();
            
            return count;
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        
        return -1;
    }
    
    public boolean exitsTable(String squema, String table) {
        try {
            String sql = "SELECT COUNT(*) FROM " + squema + "." + table;
            
            ps = JdbcConnection.getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();
            
            boolean resultado = false;
            
            if (rs.first()) {
                resultado = true;
            }
            
            rs.close();
            ps.close();
            
            return resultado;
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        
        return false;
    }
    
    public List<AtributosDescricao> getListaAtributos(String schema, String tabela) {
        try {
            ps = JdbcConnection.getConnection().prepareStatement("SELECT TOP 1 * FROM " + schema + "." + tabela, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

            rs = ps.executeQuery();

            List<AtributosDescricao> lista = new ArrayList<AtributosDescricao>();

            if (rs != null) {
                //do {
                    ResultSetMetaData resultSetMetaData = rs.getMetaData();

                    for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
                        AtributosDescricao atr = new AtributosDescricao();
                        atr.setSchema(schema); //resultSetMetaData.getSchemaName(i));
                        atr.setNomeTabela(resultSetMetaData.getTableName(i));
                        atr.setNomeAtributo(resultSetMetaData.getColumnName(i));
                        atr.setTipo(resultSetMetaData.getColumnTypeName(i));
                        atr.setTamanho(resultSetMetaData.getColumnDisplaySize(i));
                        atr.setPrecisao(resultSetMetaData.getPrecision(i));
                        atr.setScala(resultSetMetaData.getScale(i));

                        lista.add(atr);
                    }
                //} while (rs.next());
            }
            
            return lista;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }
    
    public PreparedStatement getPreparedStatement(String sql) {
        if (sql == null || sql.trim().length() <= 5) {
            return null;
        }
        
        try {
            ps = JdbcConnection.getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            
            return ps;
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        return null;
    }
    
    public ResultSet executeSQL(String sql) {
        if (sql == null || sql.trim().length() <= 5) {
            return null;
        }
        //TODO: System.out.println("SQL ERROR: " + sql);
        try {
            ps = JdbcConnection.getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();
            
            if (rs != null && rs.first()) {
                return rs;
            }
            
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        }
        return null;
    }    
    
}
